package com.sgb.kjwl.viewmodel.transport;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.ViewModel;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.LatLngBounds;
import com.amap.api.maps.model.PolylineOptions;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.route.BusRouteResult;
import com.amap.api.services.route.DrivePath;
import com.amap.api.services.route.DriveRouteResult;
import com.amap.api.services.route.DriveStep;
import com.amap.api.services.route.RideRouteResult;
import com.amap.api.services.route.RouteSearch;
import com.amap.api.services.route.TruckPath;
import com.amap.api.services.route.TruckStep;
import com.amap.api.services.route.WalkRouteResult;
import com.sgb.kjwl.R;
import com.sgb.kjwl.view.ui.activity.self.logistics.OfferOwnerActivity;
import com.sgb.kjwl.view.ui.activity.transport.HandleAdressActivity;

import java.util.ArrayList;
import java.util.List;

public class HandleAdressViewModel extends AndroidViewModel implements RouteSearch.OnRouteSearchListener {
    public HandleAdressViewModel(@NonNull Application application) {
        super(application);
    }


    private AMap aMap;
    private RouteSearch mRouteSearch;

    public void setAMap(AMap aMap) {
        this.aMap = aMap;
    }

    /**
     * 添加需要规划的路线数据
     *
     * @param activity
     * @param points   路线连接点
     */
    public void planningLoadingRoute(HandleAdressActivity activity, List<LatLonPoint> points) {
        //路线点不能少于两个
        if (points == null || points.size() < 2) {
            return;
        }
        List<LatLonPoint> passedByPoints = null;
        //设置开始位置坐标点(注意经纬度不能写反,会报错1800(只能规划到中国区域里的地图路线))
        LatLonPoint startPoint = points.get(0);
        //设置目的地坐标点
        LatLonPoint endPoint = points.get(points.size() - 1);
        //查询信息对象
        RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(startPoint, endPoint);

        if (points.size() > 2) {
            passedByPoints = new ArrayList<>();
            for (int i = 1; i < points.size() - 1; i++) {
                passedByPoints.add(points.get(i));
            }
        }
        // 对象进行初始化
        if (mRouteSearch == null)
            mRouteSearch = new RouteSearch(activity);
        // 搜索监听
        mRouteSearch.setRouteSearchListener(this);

        //设置搜索参数 1.fromAndTo 路径的起点终点  2.路径规划的策略（可选），默认为“躲避拥堵” 3.途经点，可选 4.避让区域，可选， 5.货车大小 ,默认轻型车
        RouteSearch.DriveRouteQuery query = new RouteSearch.DriveRouteQuery(
                fromAndTo,
                RouteSearch.DRIVING_SINGLE_SHORTEST,
                passedByPoints,
                null, "");

        //开始异步查询
        mRouteSearch.calculateDriveRouteAsyn(query);
    }

    @Override
    public void onBusRouteSearched(BusRouteResult busRouteResult, int i) {

    }

    @Override
    public void onDriveRouteSearched(DriveRouteResult result, int rCode) {
        if (rCode == 1000 && result != null && result.getPaths() != null && result.getPaths().size() > 0){//获取规划路线成功,获取到的是了,路线坐标点的集合
            if (aMap == null)
                return;
            List<DrivePath> paths = result.getPaths();
            //创建存储坐标点的集合
            List<LatLng> latLngs = new ArrayList<>();

            //遍历获取规划的所有路线坐标点
            for (DriveStep mDriveStep : paths.get(0).getSteps()) {
                for (LatLonPoint mLatLonPoint : mDriveStep.getPolyline()) {
                    latLngs.add(new LatLng(mLatLonPoint.getLatitude(), mLatLonPoint.getLongitude()));
                }
            }
            //绘制规划路径路线
            aMap.addPolyline(new PolylineOptions()
                    //路线坐标点的集合
                    .addAll(latLngs)
                    //线的宽度
                    .width(20)
                    //设置纹理样式
                    .setCustomTexture(BitmapDescriptorFactory.fromResource(R.drawable.map_alr))
            );
            //显示完整地图路线
            LatLngBounds.Builder builder = new LatLngBounds.Builder();
            for (int i = 0; i < latLngs.size(); i++) {
                builder.include(latLngs.get(i));
            }
            //显示全部路径点,第二个参数是四周留空宽度
            aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 200));
        }

    }

    @Override
    public void onWalkRouteSearched(WalkRouteResult walkRouteResult, int i) {

    }

    @Override
    public void onRideRouteSearched(RideRouteResult rideRouteResult, int i) {

    }
}
